<!-- 1903010328 查询通行证 -->
<?php
include "../../inc/log/adsession.php";
include "../../inc/data/connAdmin.php";
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
		require "../../vendor/autoload.php";
		use PhpOffice\PhpSpreadsheet\IOFactory;
		//  1903010328读取,实例化
		$file = $_FILES["taskfile"]["tmp_name"];
		$spreadSheet = IOFactory::load($file);
		// 获取表
		$workSheet = $spreadSheet->getSheet(0);
		$rows = $workSheet->getHighestRow();
		for ($i=3; $i <= $rows; $i++) { 
			 $sql = "insert into mamx_task,mamx_teacher,mamx_course
			 		(taskterm,tasktime,taskroom,teid,tename,classname,cocode,coname,cograde,majorname)
			 		values
			 		(?,?,?,?,?,?,?,?,?,?)
			 		where mamx_task.coid=mamx_course.coid
			 		and mamx_task.teid=mamx_teacher.teid";
			if ($stmt=$conn->prepare($sql)) {
			 	$teId=$workSheet->getCell("b$i")->getValue();
			 	$teName=$workSheet->getCell("c$i")->getValue();
			 	$cCode=$workSheet->getCell("d$i")->getValue();
			 	$cName=$workSheet->getCell("e$i")->getValue();
			 	$cGrade=$workSheet->getCell("f$i")->getValue();
			 	$majorName=$workSheet->getCell("g$i")->getValue();
			 	$taskTerm=$workSheet->getCell("h$i")->getValue();
			 	$stuClass=$workSheet->getCell("i$i")->getValue();
			 	$taskTime=$workSheet->getCell("j$i")->getValue();
			 	$taskRoom=$workSheet->getCell("k$i")->getValue();
			 	$stmt->bind_param("iiiissisis",$taskTerm,$taskTime,$taskRoom,$teId,$teName,$stuClass,$cCode,$cName,$cGrade,$majorName);
			 	$stmt->execute();
			 	if ($stmt->affected_rows>0) {
			 		$count+=1;
			 	}
			 }
		}
		// 打印值
		if($count>0){
			echo <<<END
			<script type="text/javascript">
			alert("成功添加$count 个教学任务！");
			history.back();
			</script>			
END;
		}
		else{
			echo <<<END
			<script type="text/javascript">
			alert("录入失败，请重新尝试！");
			history.back();
			</script>			
END;
		}
		
	?>
</body>
</html>